var areaList = []
var area=$("#area"), province=$("#province"),city=$("#city"),town=$("#town");
/**
 * 请求后台区域列表
 */
if(areaList.length == 0){
	ajaxUtil.post({
		url : commonUtil.domain() +'/admin/wxwork/area/list'
		}, function(data) {
			areaList = data.data;
			for(var i = 0; i < areaList.length; i ++){
				addEle(area,areaList[i].areaName,areaList[i].areaCode)
			}
	});
}
/**
 * 根据code获取code下属的list
 * @param regionCode
 * @param provinceCode
 * @param cityCode
 * @returns
 */
function getListByCode(regionCode, provinceCode, cityCode){
	if(regionCode != null){
		for(var i = 0; i < areaList.length; i ++){
			if(areaList[i].areaCode == regionCode){
				var provinceArray = areaList[i].provinceArray;
				if(provinceCode == null){
					return provinceArray;
				}
				for(var i = 0; i < provinceList.length; i ++){
					if(provinceList[i].provinceCode == provinceCode){
						var cityList = provinceList[i].mallCityList;
						if(cityCode == null){
							return cityList;
						}
						for(var i = 0; i < cityList.length; i ++){
							if(cityList[i].cityCode == cityCode){
								return cityList[i].mallAreaList;
							}
						}
					}
				}
			}
		}
	}
}
/**
 * 下拉框填充值
 * @param ele
 * @param value
 * @param code
 * @returns
 */
function addEle(ele,value,code){
    var optionStr="";
    optionStr="<option data-code="+code+" value="+value+">"+value+"</option>";
    ele.append(optionStr);
}
/**
 * 下拉框所有选项全部移除
 * @param ele
 * @returns
 */
function removeEle(ele){
    ele.find("option").remove();
    var optionStar="<option value="+"请选择"+">"+"请选择"+"</option>";
    ele.append(optionStar);
}
/**
 * 大区下拉框change事件
 * @returns
 */
area.on("change",function(){
	var areaCode = $('#area option:selected').data('code');
	window.areaCode = areaCode;
	var dataList = getListByCode(areaCode, null, null);
    if(dataList == null || dataList.length == 0){
    	return;
    }
    console.info(dataList);
	removeEle(province);
    removeEle(city);
    removeEle(town);
    $.each(dataList,function(i,item){
		addEle(province, item.split("-")[1], item.split("-")[0]);
	})
});
province.on("change",function(){
	var provinceCode = $('#province option:selected').data('code');
	window.provinceCode = provinceCode;
	var dataList = getListByCode(areaCode, provinceCode, null);
	if(dataList == null || dataList.length == 0){
    	return;
    }
    removeEle(city);
    removeEle(town);
    $.each(dataList,function(i,item){
		addEle(city, item.cityName, item.cityCode);
	})
});
city.on("change",function(){
	var cityCode = $('#city option:selected').data('code');
	window.cityCode = cityCode;
	var dataList = getListByCode(areaCode, provinceCode, cityCode);
	if(dataList == null || dataList.length == 0){
    	return;
    }
    removeEle(town);
    $.each(dataList,function(i,item){
		addEle(town, item.areaName, item.areaCode);
	})
});